<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>

	
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="Content-Language" content="en-us">

		<title>Django | Writing your first Django app, part 4 | Django Documentation</title>

		<meta name="ROBOTS" content="ALL">
		<meta http-equiv="imagetoolbar" content="no">
		<meta name="MSSmartTagsPreventParsing" content="true">
		<meta name="Copyright" content="This site's design and contents Copyright (c) 2005  Lawrence Journal-World.">

		<meta name="keywords" content="Python, Django, framework, open-source">
		<meta name="description" content="Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.">

		<link href="Django%20%7C%20Writing%20your%20first%20Django%20app,%20part%204%20%7C%20Django%20Documentation_files/base.css" rel="stylesheet" type="text/css" media="screen">
		<link href="Django%20%7C%20Writing%20your%20first%20Django%20app,%20part%204%20%7C%20Django%20Documentation_files/print.css" rel="stylesheet" type="text/css" media="print">
      
  
  <style type="text/css" media="screen">
    #docs-search {
      color: #000;
      float: right;
    }
    #docs-search form {
      font-size: 92%;
      margin: 0;
      padding: 1em 1em 0;
      white-space: nowrap;
    }
    form.search ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    form.search li {
      display: inline;
      padding-right: 1em;
    }
    form.search .query {
      width: 18em;
    }
  </style>
  <link rel="stylesheet" href="Django%20%7C%20Writing%20your%20first%20Django%20app,%20part%204%20%7C%20Django%20Documentation_files/pygments.css" type="text/css">

	</head><body id="documentation" class="default">

	<div id="container">
		<div id="header">
			<h1 id="logo"><a href="http://www.djangoproject.com/"><img src="Django%20%7C%20Writing%20your%20first%20Django%20app,%20part%204%20%7C%20Django%20Documentation_files/hdr_logo.gif" alt="Django"></a></h1>
			<ul id="nav-global">
				<li id="nav-homepage"><a href="http://www.djangoproject.com/">Home</a></li>
				<li id="nav-download"><a href="http://www.djangoproject.com/download/">Download</a></li>
				<li id="nav-documentation"><a href="http://docs.djangoproject.com/">Documentation</a></li>
				<li id="nav-weblog"><a href="http://www.djangoproject.com/weblog/">Weblog</a></li>
				<li id="nav-community"><a href="http://www.djangoproject.com/community/">Community</a></li>
				<li id="nav-code"><a href="http://code.djangoproject.com/">Code</a></li>
			</ul>
		</div>
		<!-- END Header -->
		<div id="billboard">
  <h2><a href="http://docs.djangoproject.com/en/dev/">Django documentation</a></h2>
</div>
		<div id="columnwrap">
			
		<div id="content-main">
		


  <h2 class="deck">
  
    This document is for Django's SVN release, which can be
    significantly different from previous releases. Get old docs here: 
      <a href="http://docs.djangoproject.com/en/1.0/intro/tutorial04/">Django 1.0</a>
  
  </h2>
  <div class="section" id="s-writing-your-first-django-app-part-4">
<span id="s-intro-tutorial04"></span><span id="writing-your-first-django-app-part-4"></span><span id="intro-tutorial04"></span><h1>Writing your first Django app, part 4<a class="headerlink" href="#writing-your-first-django-app-part-4" title="Permalink to this headline">¶</a></h1>
<p>This tutorial begins where <a class="reference external" href="http://docs.djangoproject.com/en/dev/intro/tutorial03/#intro-tutorial03"><em>Tutorial 3</em></a> left off. We’re
continuing the Web-poll application and will focus on simple form processing and
cutting down our code.</p>
<div class="section" id="s-write-a-simple-form">
<span id="write-a-simple-form"></span><h2>Write a simple form<a class="headerlink" href="#write-a-simple-form" title="Permalink to this headline">¶</a></h2>
<p>Let’s update our poll detail template (“polls/detail.html”) from the last
tutorial, so that the template contains an HTML <tt class="docutils literal"><span class="pre">&lt;form&gt;</span></tt> element:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;h1&gt;</span><span class="cp">{{</span> <span class="nv">poll.question</span> <span class="cp">}}</span><span class="nt">&lt;/h1&gt;</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">error_message</span> <span class="cp">%}</span><span class="nt">&lt;p&gt;&lt;strong&gt;</span><span class="cp">{{</span> <span class="nv">error_message</span> <span class="cp">}}</span><span class="nt">&lt;/strong&gt;&lt;/p&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="nt">&lt;form</span> <span class="na">action=</span><span class="s">"vote/"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">choice</span> <span class="k">in</span> <span class="nv">poll.choice_set.all</span> <span class="cp">%}</span>
    <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">name=</span><span class="s">"choice"</span> <span class="na">id=</span><span class="s">"choice</span><span class="cp">{{</span> <span class="nb">forloop</span><span class="nv">.counter</span> <span class="cp">}}</span><span class="s">"</span> <span class="na">value=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">choice.id</span> <span class="cp">}}</span><span class="s">"</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;label</span> <span class="na">for=</span><span class="s">"choice</span><span class="cp">{{</span> <span class="nb">forloop</span><span class="nv">.counter</span> <span class="cp">}}</span><span class="s">"</span><span class="nt">&gt;</span><span class="cp">{{</span> <span class="nv">choice.choice</span> <span class="cp">}}</span><span class="nt">&lt;/label&gt;&lt;br</span> <span class="nt">/&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">value=</span><span class="s">"Vote"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/form&gt;</span>
</pre></div>
</div>
<p>A quick rundown:</p>
<ul class="simple">
<li>The above template displays a radio button for each poll choice. The
<tt class="docutils literal"><span class="pre">value</span></tt> of each radio button is the associated poll choice's ID. The
<tt class="docutils literal"><span class="pre">name</span></tt> of each radio button is <tt class="docutils literal"><span class="pre">"choice"</span></tt>. That means, when somebody
selects one of the radio buttons and submits the form, it'll send the
POST data <tt class="docutils literal"><span class="pre">choice=3</span></tt>. This is HTML Forms 101.</li>
<li>We set the form's <tt class="docutils literal"><span class="pre">action</span></tt> to <tt class="docutils literal"><span class="pre">vote/</span></tt>, and we set <tt class="docutils literal"><span class="pre">method="post"</span></tt>.
Using <tt class="docutils literal"><span class="pre">method="post"</span></tt> (as opposed to <tt class="docutils literal"><span class="pre">method="get"</span></tt>) is very
important, because the act of submitting this form will alter data
server-side. Whenever you create a form that alters data server-side, use
<tt class="docutils literal"><span class="pre">method="post"</span></tt>. This tip isn't specific to Django; it's just good Web
development practice.</li>
<li><tt class="docutils literal"><span class="pre">forloop.counter</span></tt> indicates how many times the <a class="reference external" href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ttag-for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> tag has gone
through its loop</li>
</ul>
<p>Now, let's create a Django view that handles the submitted data and does
something with it. Remember, in <a class="reference external" href="http://docs.djangoproject.com/en/dev/intro/tutorial03/#intro-tutorial03"><em>Tutorial 3</em></a>, we
created a URLconf for the polls application that includes this line:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="s">r'^(?P&lt;poll_id&gt;\d+)/vote/$'</span><span class="p">,</span> <span class="s">'vote'</span><span class="p">),</span>
</pre></div>
</div>
<p>So let's create a <tt class="docutils literal"><span class="pre">vote()</span></tt> function in <tt class="docutils literal"><span class="pre">mysite/polls/views.py</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.shortcuts</span> <span class="k">import</span> <span class="n">get_object_or_404</span><span class="p">,</span> <span class="n">render_to_response</span>
<span class="k">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseRedirect</span>
<span class="k">from</span> <span class="nn">django.core.urlresolvers</span> <span class="k">import</span> <span class="n">reverse</span>
<span class="k">from</span> <span class="nn">mysite.polls.models</span> <span class="k">import</span> <span class="n">Choice</span><span class="p">,</span> <span class="n">Poll</span>
<span class="c"># ...</span>
<span class="k">def</span> <span class="nf">vote</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">):</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">selected_choice</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">choice_set</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">[</span><span class="s">'choice'</span><span class="p">])</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">KeyError</span><span class="p">,</span> <span class="n">Choice</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">):</span>
        <span class="c"># Redisplay the poll voting form.</span>
        <span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">'polls/detail.html'</span><span class="p">,</span> <span class="p">{</span>
            <span class="s">'poll'</span><span class="p">:</span> <span class="n">p</span><span class="p">,</span>
            <span class="s">'error_message'</span><span class="p">:</span> <span class="s">"You didn't select a choice."</span><span class="p">,</span>
        <span class="p">})</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">selected_choice</span><span class="o">.</span><span class="n">votes</span> <span class="o">+=</span> <span class="mf">1</span>
        <span class="n">selected_choice</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="c"># Always return an HttpResponseRedirect after successfully dealing</span>
        <span class="c"># with POST data. This prevents data from being posted twice if a</span>
        <span class="c"># user hits the Back button.</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s">'mysite.polls.views.results'</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">id</span><span class="p">,)))</span>
</pre></div>
</div>
<p>This code includes a few things we haven't covered yet in this tutorial:</p>
<ul>
<li><p class="first"><a title="django.http.HttpRequest.POST" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.POST"><tt class="xref docutils literal"><span class="pre">request.POST</span></tt></a> is a dictionary-like
object that lets you access submitted data by key name. In this case,
<tt class="docutils literal"><span class="pre">request.POST['choice']</span></tt> returns the ID of the selected choice, as a
string. <a title="django.http.HttpRequest.POST" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.POST"><tt class="xref docutils literal"><span class="pre">request.POST</span></tt></a> values are
always strings.</p>
<p>Note that Django also provides <a title="django.http.HttpRequest.GET" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.GET"><tt class="xref docutils literal"><span class="pre">request.GET</span></tt></a> for accessing GET data in the same way --
but we're explicitly using <a title="django.http.HttpRequest.POST" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.POST"><tt class="xref docutils literal"><span class="pre">request.POST</span></tt></a> in our code, to ensure that data is only
altered via a POST call.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">request.POST['choice']</span></tt> will raise <tt class="xref docutils literal"><span class="pre">KeyError</span></tt> if <tt class="docutils literal"><span class="pre">choice</span></tt> wasn't
provided in POST data. The above code checks for <tt class="xref docutils literal"><span class="pre">KeyError</span></tt> and
redisplays the poll form with an error message if <tt class="docutils literal"><span class="pre">choice</span></tt> isn't given.</p>
</li>
<li><p class="first">After incrementing the choice count, the code returns an
<a title="django.http.HttpResponseRedirect" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponseRedirect"><tt class="xref docutils literal"><span class="pre">HttpResponseRedirect</span></tt></a> rather than a normal
<a title="django.http.HttpResponse" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse"><tt class="xref docutils literal"><span class="pre">HttpResponse</span></tt></a>.
<a title="django.http.HttpResponseRedirect" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponseRedirect"><tt class="xref docutils literal"><span class="pre">HttpResponseRedirect</span></tt></a> takes a single argument: the
URL to which the user will be redirected (see the following point for how
we construct the URL in this case).</p>
<p>As the Python comment above points out, you should always return an
<a title="django.http.HttpResponseRedirect" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponseRedirect"><tt class="xref docutils literal"><span class="pre">HttpResponseRedirect</span></tt></a> after successfully dealing with
POST data. This tip isn't specific to Django; it's just good Web
development practice.</p>
</li>
<li><p class="first">We are using the <a title="django.core.urlresolvers.reverse" class="reference external" href="http://docs.djangoproject.com/en/dev/topics/http/urls/#django.core.urlresolvers.reverse"><tt class="xref docutils literal"><span class="pre">reverse()</span></tt></a> function in the
<a title="django.http.HttpResponseRedirect" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponseRedirect"><tt class="xref docutils literal"><span class="pre">HttpResponseRedirect</span></tt></a> constructor in this example.
This function helps avoid having to hardcode a URL in the view function.
It is given the name of the view that we want to pass control to and the
variable portion of the URL pattern that points to that view. In this
case, using the URLconf we set up in Tutorial 3, this
<a title="django.core.urlresolvers.reverse" class="reference external" href="http://docs.djangoproject.com/en/dev/topics/http/urls/#django.core.urlresolvers.reverse"><tt class="xref docutils literal"><span class="pre">reverse()</span></tt></a> call will return a string like</p>
<div class="highlight-python"><div class="highlight"><pre><span class="s">'/polls/3/results/'</span>
</pre></div>
</div>
<p>... where the <tt class="docutils literal"><span class="pre">3</span></tt> is the value of <tt class="docutils literal"><span class="pre">p.id</span></tt>. This redirected URL will
then call the <tt class="docutils literal"><span class="pre">'results'</span></tt> view to display the final page. Note that you
need to use the full name of the view here (including the prefix).</p>
</li>
</ul>
<p>As mentioned in Tutorial 3, <tt class="docutils literal"><span class="pre">request</span></tt> is a <a title="django.http.HttpRequest" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest"><tt class="xref docutils literal"><span class="pre">HttpRequest</span></tt></a>
object. For more on <a title="django.http.HttpRequest" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest"><tt class="xref docutils literal"><span class="pre">HttpRequest</span></tt></a> objects, see the
<a class="reference external" href="http://docs.djangoproject.com/en/dev/ref/request-response/#ref-request-response"><em>request and response documentation</em></a>.</p>
<p>After somebody votes in a poll, the <tt class="docutils literal"><span class="pre">vote()</span></tt> view redirects to the results
page for the poll. Let's write that view:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">results</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">):</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">'polls/results.html'</span><span class="p">,</span> <span class="p">{</span><span class="s">'poll'</span><span class="p">:</span> <span class="n">p</span><span class="p">})</span>
</pre></div>
</div>
<p>This is almost exactly the same as the <tt class="docutils literal"><span class="pre">detail()</span></tt> view from <a class="reference external" href="http://docs.djangoproject.com/en/dev/intro/tutorial03/#intro-tutorial03"><em>Tutorial 3</em></a>. The only difference is the template name. We'll fix this
redundancy later.</p>
<p>Now, create a <tt class="docutils literal"><span class="pre">results.html</span></tt> template:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="nt">&lt;h1&gt;</span><span class="cp">{{</span> <span class="nv">poll.question</span> <span class="cp">}}</span><span class="nt">&lt;/h1&gt;</span>

<span class="nt">&lt;ul&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">choice</span> <span class="k">in</span> <span class="nv">poll.choice_set.all</span> <span class="cp">%}</span>
    <span class="nt">&lt;li&gt;</span><span class="cp">{{</span> <span class="nv">choice.choice</span> <span class="cp">}}</span> -- <span class="cp">{{</span> <span class="nv">choice.votes</span> <span class="cp">}}</span> vote<span class="cp">{{</span> <span class="nv">choice.votes</span><span class="o">|</span><span class="nf">pluralize</span> <span class="cp">}}</span><span class="nt">&lt;/li&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="nt">&lt;/ul&gt;</span>
</pre></div>
</div>
<p>Now, go to <tt class="docutils literal"><span class="pre">/polls/1/</span></tt> in your browser and vote in the poll. You should see a
results page that gets updated each time you vote. If you submit the form
without having chosen a choice, you should see the error message.</p>
</div>
<div class="section" id="s-use-generic-views-less-code-is-better">
<span id="use-generic-views-less-code-is-better"></span><h2>Use generic views: Less code is better<a class="headerlink" href="#use-generic-views-less-code-is-better" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">detail()</span></tt> (from <a class="reference external" href="http://docs.djangoproject.com/en/dev/intro/tutorial03/#intro-tutorial03"><em>Tutorial 3</em></a>) and <tt class="docutils literal"><span class="pre">results()</span></tt>
views are stupidly simple -- and, as mentioned above, redundant. The <tt class="docutils literal"><span class="pre">index()</span></tt>
view (also from Tutorial 3), which displays a list of polls, is similar.</p>
<p>These views represent a common case of basic Web development: getting data from
the database according to a parameter passed in the URL, loading a template and
returning the rendered template. Because this is so common, Django provides a
shortcut, called the "generic views" system.</p>
<p>Generic views abstract common patterns to the point where you don't even need
to write Python code to write an app.</p>
<p>Let's convert our poll app to use the generic views system, so we can delete a
bunch of our own code. We'll just have to take a few steps to make the
conversion. We will:</p>
<ol class="arabic simple">
<li>Convert the URLconf.</li>
<li>Rename a few templates.</li>
<li>Delete some the old, now unneeded views.</li>
<li>Fix up URL handling for the new views.</li>
</ol>
<p>Read on for details.</p>
<div class="admonition-why-the-code-shuffle admonition">
<p class="first admonition-title">Why the code-shuffle?</p>
<p>Generally, when writing a Django app, you'll evaluate whether generic views
are a good fit for your problem, and you'll use them from the beginning,
rather than refactoring your code halfway through. But this tutorial
intentionally has focused on writing the views "the hard way" until now, to
focus on core concepts.</p>
<p class="last">You should know basic math before you start using a calculator.</p>
</div>
<p>First, open the <tt class="docutils literal"><span class="pre">polls/urls.py</span></tt> URLconf. It looks like this, according to the
tutorial so far:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.conf.urls.defaults</span> <span class="k">import</span> <span class="o">*</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">'mysite.polls.views'</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^$'</span><span class="p">,</span> <span class="s">'index'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^(?P&lt;poll_id&gt;\d+)/$'</span><span class="p">,</span> <span class="s">'detail'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^(?P&lt;poll_id&gt;\d+)/results/$'</span><span class="p">,</span> <span class="s">'results'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^(?P&lt;poll_id&gt;\d+)/vote/$'</span><span class="p">,</span> <span class="s">'vote'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Change it like so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.conf.urls.defaults</span> <span class="k">import</span> <span class="o">*</span>
<span class="k">from</span> <span class="nn">mysite.polls.models</span> <span class="k">import</span> <span class="n">Poll</span>

<span class="n">info_dict</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">'queryset'</span><span class="p">:</span> <span class="n">Poll</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">(),</span>
<span class="p">}</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^$'</span><span class="p">,</span> <span class="s">'django.views.generic.list_detail.object_list'</span><span class="p">,</span> <span class="n">info_dict</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^(?P&lt;object_id&gt;\d+)/$'</span><span class="p">,</span> <span class="s">'django.views.generic.list_detail.object_detail'</span><span class="p">,</span> <span class="n">info_dict</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r'^(?P&lt;object_id&gt;\d+)/results/$'</span><span class="p">,</span> <span class="s">'django.views.generic.list_detail.object_detail'</span><span class="p">,</span> <span class="nb">dict</span><span class="p">(</span><span class="n">info_dict</span><span class="p">,</span> <span class="n">template_name</span><span class="o">=</span><span class="s">'polls/results.html'</span><span class="p">),</span> <span class="s">'poll_results'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^(?P&lt;poll_id&gt;\d+)/vote/$'</span><span class="p">,</span> <span class="s">'mysite.polls.views.vote'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>We're using two generic views here:
<tt class="xref docutils literal"><span class="pre">object_list()</span></tt> and
<tt class="xref docutils literal"><span class="pre">object_detail()</span></tt>. Respectively, those two
views abstract the concepts of "display a list of objects" and "display a detail
page for a particular type of object."</p>
<ul class="simple">
<li>Each generic view needs to know what data it will be acting upon. This
data is provided in a dictionary. The <tt class="docutils literal"><span class="pre">queryset</span></tt> key in this dictionary
points to the list of objects to be manipulated by the generic view.</li>
<li>The <tt class="xref docutils literal"><span class="pre">object_detail()</span></tt> generic view
expects the ID value captured from the URL to be called <tt class="docutils literal"><span class="pre">"object_id"</span></tt>,
so we've changed <tt class="docutils literal"><span class="pre">poll_id</span></tt> to <tt class="docutils literal"><span class="pre">object_id</span></tt> for the generic views.</li>
<li>We've added a name, <tt class="docutils literal"><span class="pre">poll_results</span></tt>, to the results view so that we have
a way to refer to its URL later on (see the documentation about
<a class="reference external" href="http://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns"><em>naming URL patterns</em></a> for information). We're
also using the <tt class="xref docutils literal"><span class="pre">url()</span></tt> function from
<tt class="xref docutils literal"><span class="pre">django.conf.urls.defaults</span></tt> here. It's a good habit to use
<tt class="xref docutils literal"><span class="pre">url()</span></tt> when you are providing a pattern
name like this.</li>
</ul>
<p>By default, the <tt class="xref docutils literal"><span class="pre">object_detail()</span></tt> generic
view uses a template called <tt class="docutils literal"><span class="pre">&lt;app</span> <span class="pre">name&gt;/&lt;model</span> <span class="pre">name&gt;_detail.html</span></tt>. In our
case, it'll use the template <tt class="docutils literal"><span class="pre">"polls/poll_detail.html"</span></tt>. Thus, rename your
<tt class="docutils literal"><span class="pre">polls/detail.html</span></tt> template to <tt class="docutils literal"><span class="pre">polls/poll_detail.html</span></tt>, and change the
<a title="django.shortcuts.render_to_response" class="reference external" href="http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.render_to_response"><tt class="xref docutils literal"><span class="pre">render_to_response()</span></tt></a> line in <tt class="docutils literal"><span class="pre">vote()</span></tt>.</p>
<p>Similarly, the <tt class="xref docutils literal"><span class="pre">object_list()</span></tt> generic
view uses a template called <tt class="docutils literal"><span class="pre">&lt;app</span> <span class="pre">name&gt;/&lt;model</span> <span class="pre">name&gt;_list.html</span></tt>. Thus, rename
<tt class="docutils literal"><span class="pre">polls/index.html</span></tt> to <tt class="docutils literal"><span class="pre">polls/poll_list.html</span></tt>.</p>
<p>Because we have more than one entry in the URLconf that uses
<tt class="xref docutils literal"><span class="pre">object_detail()</span></tt> for the polls app, we
manually specify a template name for the results view:
<tt class="docutils literal"><span class="pre">template_name='polls/results.html'</span></tt>. Otherwise, both views would use the same
template. Note that we use <tt class="docutils literal"><span class="pre">dict()</span></tt> to return an altered dictionary in place.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p><tt class="xref docutils literal"><span class="pre">django.db.models.QuerySet.all()</span></tt> is lazy</p>
<p>It might look a little frightening to see <tt class="docutils literal"><span class="pre">Poll.objects.all()</span></tt> being used
in a detail view which only needs one <tt class="docutils literal"><span class="pre">Poll</span></tt> object, but don't worry;
<tt class="docutils literal"><span class="pre">Poll.objects.all()</span></tt> is actually a special object called a
<a title="django.db.models.QuerySet" class="reference external" href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.QuerySet"><tt class="xref docutils literal"><span class="pre">QuerySet</span></tt></a>, which is "lazy" and doesn't hit your
database until it absolutely has to. By the time the database query happens,
the <tt class="xref docutils literal"><span class="pre">object_detail()</span></tt> generic view
will have narrowed its scope down to a single object, so the eventual query
will only select one row from the database.</p>
<p class="last">If you'd like to know more about how that works, The Django database API
documentation <a class="reference external" href="http://docs.djangoproject.com/en/dev/topics/db/queries/#querysets-are-lazy"><em>explains the lazy nature of QuerySet objects</em></a>.</p>
</div>
<p>In previous parts of the tutorial, the templates have been provided with a
context that contains the <tt class="docutils literal"><span class="pre">poll</span></tt> and <tt class="docutils literal"><span class="pre">latest_poll_list</span></tt> context variables.
However, the generic views provide the variables <tt class="docutils literal"><span class="pre">object</span></tt> and <tt class="docutils literal"><span class="pre">object_list</span></tt>
as context. Therefore, you need to change your templates to match the new
context variables. Go through your templates, and modify any reference to
<tt class="docutils literal"><span class="pre">latest_poll_list</span></tt> to <tt class="docutils literal"><span class="pre">object_list</span></tt>, and change any reference to <tt class="docutils literal"><span class="pre">poll</span></tt>
to <tt class="docutils literal"><span class="pre">object</span></tt>.</p>
<p>You can now delete the <tt class="docutils literal"><span class="pre">index()</span></tt>, <tt class="docutils literal"><span class="pre">detail()</span></tt> and <tt class="docutils literal"><span class="pre">results()</span></tt> views
from <tt class="docutils literal"><span class="pre">polls/views.py</span></tt>. We don't need them anymore -- they have been replaced
by generic views.</p>
<p>The <tt class="docutils literal"><span class="pre">vote()</span></tt> view is still required. However, it must be modified to match the
new context variables. In the <a title="django.shortcuts.render_to_response" class="reference external" href="http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.render_to_response"><tt class="xref docutils literal"><span class="pre">render_to_response()</span></tt></a> call,
rename the <tt class="docutils literal"><span class="pre">poll</span></tt> context variable to <tt class="docutils literal"><span class="pre">object</span></tt>.</p>
<p>The last thing to do is fix the URL handling to account for the use of generic
views. In the vote view above, we used the
<a title="django.core.urlresolvers.reverse" class="reference external" href="http://docs.djangoproject.com/en/dev/topics/http/urls/#django.core.urlresolvers.reverse"><tt class="xref docutils literal"><span class="pre">reverse()</span></tt></a> function to avoid hard-coding our
URLs. Now that we've switched to a generic view, we'll need to change the
<a title="django.core.urlresolvers.reverse" class="reference external" href="http://docs.djangoproject.com/en/dev/topics/http/urls/#django.core.urlresolvers.reverse"><tt class="xref docutils literal"><span class="pre">reverse()</span></tt></a> call to point back to our new generic
view. We can't simply use the view function anymore -- generic views can be (and
are) used multiple times -- but we can use the name we've given:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s">'poll_results'</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">id</span><span class="p">,)))</span>
</pre></div>
</div>
<p>Run the server, and use your new polling app based on generic views.</p>
<p>For full details on generic views, see the <a class="reference external" href="http://docs.djangoproject.com/en/dev/topics/http/generic-views/#topics-http-generic-views"><em>generic views documentation</em></a>.</p>
</div>
<div class="section" id="s-coming-soon">
<span id="coming-soon"></span><h2>Coming soon<a class="headerlink" href="#coming-soon" title="Permalink to this headline">¶</a></h2>
<p>The tutorial ends here for the time being. Future installments of the tutorial
will cover:</p>
<ul class="simple">
<li>Advanced form processing</li>
<li>Using the RSS framework</li>
<li>Using the cache framework</li>
<li>Using the comments framework</li>
<li>Advanced admin features: Permissions</li>
<li>Advanced admin features: Custom JavaScript</li>
</ul>
<p>In the meantime, you might want to check out some pointers on <a class="reference external" href="http://docs.djangoproject.com/en/dev/intro/whatsnext/#intro-whatsnext"><em>where to go
from here</em></a></p>
</div>
</div>



<div id="content-secondary">
  <h2 id="comments">Questions/Feedback</h2>
  <p>Having trouble? We'd like to help!</p>
  <ul>
    <li>
      Try the <a href="http://docs.djangoproject.com/en/dev/faq/">FAQ</a> — it's got answers to many common
      questions.
    </li>
    <li>
      Search for information in the <a href="http://groups.google.com/group/django-users/">archives of the
      django-users mailing list</a>, or <a href="http://groups.google.com/group/django-users/">post a question</a>.
    </li>
    <li>
      Ask a question in the <a href="irc://irc.freenode.net/">#django IRC
      channel</a>, or search the <a href="http://oebfare.com/logger/django/">IRC
      logs</a> to see if its been asked before.
    </li>
    <li>
      If you notice errors with this documentation, please <a href="http://code.djangoproject.com/simpleticket?component=Documentation">
      open a ticket</a> and let us know! Please only use the ticket tracker for
      criticisms and improvements on the docs. For tech support, use the
      resources above.
    </li>
  </ul>
</div>

		</div>
		<!-- END #content-main -->
		<div id="content-related" class="sidebar">
		
  
    <h2>Contents</h2>
    
      <ul>
<li><a class="reference external" href="">Writing your first Django app, part 4</a><ul>
<li><a class="reference external" href="#write-a-simple-form">Write a simple form</a></li>
<li><a class="reference external" href="#use-generic-views-less-code-is-better">Use generic views: Less code is better</a></li>
<li><a class="reference external" href="#coming-soon">Coming soon</a></li>
</ul>
</li>
</ul>

    
  
  
  
    <h2>Search</h2>
    
    <form action="/en/dev/search/" id="search" class="search">
  <div>
    <input name="cx" value="009763561546736975936:e88ek0eurf4" type="hidden">
    <input name="cof" value="FORID:11" type="hidden">
    <input name="ie" value="UTF-8" type="hidden">
    <input name="hl" value="" type="hidden">
    <input style="background: rgb(255, 255, 255) url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif) no-repeat scroll left center; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="id_search_q" class="query" name="q" type="text">
    <input name="sa" class="submit" value="Search" type="submit">
    <ul>
<li><label for="id_search_as_q_0"><input checked="checked" id="id_search_as_q_0" value="more:dev_docs" name="as_q" type="radio"> Latest</label></li>
<li><label for="id_search_as_q_1"><input id="id_search_as_q_1" value="more:1.0_docs" name="as_q" type="radio"> 1.0</label></li>
<li><label for="id_search_as_q_2"><input id="id_search_as_q_2" value="more:0.96_docs" name="as_q" type="radio"> 0.96</label></li>
<li><label for="id_search_as_q_3"><input id="id_search_as_q_3" value="more:all_docs" name="as_q" type="radio"> All</label></li>
</ul>
  </div>
</form>
<script type="text/javascript" src="Django%20%7C%20Writing%20your%20first%20Django%20app,%20part%204%20%7C%20Django%20Documentation_files/brand.html"></script>
  
  
  
    <h2>Browse</h2>
    <ul>
      
        
          <li>Prev: <a href="http://docs.djangoproject.com/en/dev/intro/tutorial03/">Writing your first Django app, part 3</a></li>
        
        
          <li>Next: <a href="http://docs.djangoproject.com/en/dev/intro/whatsnext/">What to read next</a></li>
        
        <li><a href="http://docs.djangoproject.com/en/dev/contents/">Table of contents</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/dev/genindex/">General Index</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/dev/modindex/">Global Module Index</a></li>
        
      
    </ul>
  
  
  
    <h2>You are here:</h2>
    <ul>
      
        <li>
          <a href="http://docs.djangoproject.com/en/dev/">Django dev documentation</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/dev/intro/">Getting started</a>
          
          <ul><li>Writing your first Django app, part 4</li></ul>
          </li></ul>
        </li>
      
    </ul>
  
  
  
    <h3>Last update:</h3>
    <div>May 23, 2009, 8 a.m. (<a href="http://www.timeanddate.com/worldclock/city.html?n=64">CDT</a>)</div>
  

		</div>
		<!-- END #content-related -->

		</div>
		<!-- END #content -->
		<div id="footer">
			<p>© 2005-2009 <a href="http://www.djangoproject.com/foundation/">Django Software Foundation</a> unless otherwise noted. Django is a registered trademark of the Django Software Foundation. 
			Hosting graciously provided by <a href="http://mediatemple.net/">
			<img style="vertical-align: middle; position: relative; top: -1px;" src="Django%20%7C%20Writing%20your%20first%20Django%20app,%20part%204%20%7C%20Django%20Documentation_files/mt.png" alt="media temple"></a>
			</p>
		</div>
		<!-- END #footer -->
	</div>
	<!-- END #container -->
	</body></html>